package com.niuwa.p2p.service.promotion.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DateUtil;
import com.niuwa.p2p.dao.promotion.PromotionGiftMapper;
import com.niuwa.p2p.dao.promotion.PromotionMapper;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.gift.PromotionGiftinfo;
import com.niuwa.p2p.entity.promotion.Promotion;
import com.niuwa.p2p.service.promotion.PromotionService;

@Service
public class PromotionServiceImpl implements PromotionService {

	private Logger LOGGER = Logger.getLogger(PromotionServiceImpl.class);
	
	@Resource
	private PromotionMapper promotionDao;
	
	@Resource
	private PromotionGiftMapper promotionGiftDao;

	@Override
	public PageBean<Promotion> searchPromotionPage(
			Map<String, Object> paramMap) { 
		
		List<Promotion> promotionList = new ArrayList<Promotion>();
		
		int totalCount = promotionDao.selectCountByParam(paramMap);
		PageBean<Promotion> page  = new PageBean<Promotion>(totalCount);
		// 当前页
		page.setPage(((PageQuery)paramMap.get(PageQuery.PAGEQUERY)).getPage());
		promotionList= promotionDao.selectByParam(paramMap);
		page.setRows(promotionList);
		return page;
		
		/**
		 * {
			PromotionVo promotionVo = new PromotionVo();
			promotionVo.setPromotionId(1);
			promotionVo.setPromotionName("活动1");
			promotionVo.setPromotionForcode("a1244");
			promotionVo.setPromotionForname("MB积分商城");
			promotionVo.setPromotionType(new Byte("1"));
			promotionVo.setStatusDesc("正在运行");
			promotionVo.setStatus(Byte.parseByte("1"));
			try {
//				promotionVo.setStartTime(DateUtil.parseStringToDate(
//						"2015-10-01 00:00:00", "yyyy-MM-dd HH:mm:ss"));
//				promotionVo.setEndTime(DateUtil.parseStringToDate(
//						"2015-10-11 00:00:00", "yyyy-MM-dd HH:mm:ss"));
				promotionVo.setStartTime("2015-10-01 00:00:00");
				promotionVo.setEndTime("2015-10-11 00:00:00");
			} catch (Exception e) {
				LOGGER.error(ExceptionUtils.getFullStackTrace(e));
			}
			PromotionVoList.add(promotionVo);
		}
		
		{
			PromotionVo promotionVo = new PromotionVo();
			
			promotionVo.setPromotionId(2);
			promotionVo.setPromotionName("活动2");
			promotionVo.setPromotionForcode("a1244");
			promotionVo.setPromotionForname("MB积分商城");
			promotionVo.setPromotionType(new Byte("1"));
			promotionVo.setStatusDesc("正在运行");
			promotionVo.setStatus(Byte.parseByte("1"));
			try {
				promotionVo.setStartTime("2015-10-11 00:00:00");
				promotionVo.setEndTime("2015-10-21 00:00:00");
			} catch (Exception e) {
				LOGGER.error(ExceptionUtils.getFullStackTrace(e));
			}
			PromotionVoList.add(promotionVo);
		}
		 */
	}

	/**
     * 活动号生成规则
     * 举例：J0151013101
     * J0 两位标识活动归属   J 积分商城 B 邦购 Y有范
     * 151013 2015-10-13
     * 1 活动类型1积分 2 折扣 3积分+折扣
     * *01 活动流水
     */
	@Override
	public String generatPromotionCode(String promotionForcode,Byte promotionType) {
		
		Map map = new HashMap();
		StringBuffer bf =new StringBuffer(promotionForcode);//JF;YF;BG
		bf.append(DateUtil.formatDateToString(new Date(),"yyMMdd"));//151013
		bf.append(promotionType);//1;2;3
		map.put("generatPromotionCode", bf.toString());
		map.put("sort", "promotion_code desc");
		List<Promotion> list = promotionDao.selectByParam(map);
		
		if(list.size()==0){
			bf.append("01");
		}else{
			Promotion p = list.get(0);
			String promotionCode = p.getPromotionCode();
			int sq = Integer.parseInt(promotionCode.substring(9, 11));
			if(sq<99){
				sq++;
			}else{
				//throw new  Exception("sequence over 99！");
			}
			DecimalFormat df = new DecimalFormat("00");  
			bf.append(df.format(sq));
		}
		
		return bf.toString();
	}

	@Override
	public int savePromotion(Promotion promotion) {
		Integer promotionId = promotion.getPromotionId();
		if(promotionId!=null){
			promotionDao.updateByPrimaryKeySelective(promotion);
		}else{
			int pid = promotionDao.insert(promotion);
			return pid;
		}
		return promotionId;
	}

}
